﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class CT_LuongThangBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();

        public List<CT_LUONGTHANG> LayTatCa()
        {
            return DB.CT_LUONGTHANGs.ToList();
        }

        public void Them(CT_LUONGTHANG a)
        {
            DB.CT_LUONGTHANGs.InsertOnSubmit(a);
            DB.SubmitChanges();
        }

        public List<CT_LUONGTHANG> LayLuongThang(int thang, int nam)
        {
            List<CT_LUONGTHANG> ls = (from a in DB.CT_LUONGTHANGs
                                      where a.Thang == thang && a.Nam == nam
                                      select a).ToList();
            return ls;
        }

        public bool KiemTraTinhLuong(int thang, int nam)
        {
            if (DB.CT_LUONGTHANGs.Any(a => a.Thang == thang && a.Nam == nam))
                return true;
            else
                return false;
        }

        public List<DGVBANGLUONG> LayBangLuong(int thang, int nam, int mapb, string hoten)
        {
            if (mapb == 0 && hoten == "")
            {
                List<DGVBANGLUONG> ls = (from a in DB.TONGCONGTHANGs
                                         from b in DB.CT_LUONGTHANGs
                                         where a.MaNV == b.MaNV && a.Thang == thang && a.Nam == nam && b.Thang == thang
                                         select new DGVBANGLUONG()
                                         {
                                             MaNV = a.MaNV,
                                             Thang = thang,
                                             Nam = nam,
                                             TenNV = b.NHANVIEN.HoTen,
                                             CongLamViec = (double)a.CongLamViec,
                                             LuongLamViec = (int)b.Luong,
                                             CongTangCa = (double)a.CongTangCa,
                                             LuongTangCa = (int)b.LuongTangCa,
                                             LuongPhuCap = (int)b.PhuCap,
                                             HieuSuat = (double)a.HieuSuatLamViec,
                                             TongLuong = (int)b.TongLuongThang
                                         }).ToList();
                return ls;
            }
            else if (mapb == 0 && hoten != "")
            {
                List<DGVBANGLUONG> ls = (from a in DB.TONGCONGTHANGs
                                         from b in DB.CT_LUONGTHANGs
                                         where a.MaNV == b.MaNV && b.Thang == thang && a.Thang == thang && a.Nam == nam && a.NHANVIEN.HoTen.Contains(@hoten)
                                         select new DGVBANGLUONG()
                                         {
                                             MaNV = a.MaNV,
                                             TenNV = b.NHANVIEN.HoTen,
                                             Thang = thang,
                                             Nam = nam,
                                             CongLamViec = (double)a.CongLamViec,
                                             LuongLamViec = (int)b.Luong,
                                             CongTangCa = (double)a.CongTangCa,
                                             LuongTangCa = (int)b.LuongTangCa,
                                             LuongPhuCap = (int)b.PhuCap,
                                             HieuSuat = (double)a.HieuSuatLamViec,
                                             TongLuong = (int)b.TongLuongThang
                                         }).ToList();

                return ls;

            }
            else if (hoten == "" && mapb != 0)
            {
                List<DGVBANGLUONG> ls = (from a in DB.TONGCONGTHANGs
                                         from b in DB.CT_LUONGTHANGs
                                         where a.MaNV == b.MaNV && b.Thang == thang&& a.Thang == thang && a.Nam == nam && a.NHANVIEN.MaPB == mapb
                                         select new DGVBANGLUONG()
                                         {
                                             MaNV = a.MaNV,
                                             TenNV = b.NHANVIEN.HoTen,
                                             Thang = thang,
                                             Nam = nam,
                                             CongLamViec = (double)a.CongLamViec,
                                             LuongLamViec = (int)b.Luong,
                                             CongTangCa = (double)a.CongTangCa,
                                             LuongTangCa = (int)b.LuongTangCa,
                                             LuongPhuCap = (int)b.PhuCap,
                                             HieuSuat = (double)a.HieuSuatLamViec,
                                             TongLuong = (int)b.TongLuongThang
                                         }).ToList();

                return ls;
            }
            else
            {
                List<DGVBANGLUONG> ls = (from a in DB.TONGCONGTHANGs
                                         from b in DB.CT_LUONGTHANGs
                                         where a.MaNV == b.MaNV && a.Thang == thang && b.Thang == thang && a.Nam == nam && a.NHANVIEN.MaPB == mapb && a.NHANVIEN.HoTen.Contains(@hoten)
                                         select new DGVBANGLUONG()
                                         {
                                             MaNV = a.MaNV,
                                             TenNV = b.NHANVIEN.HoTen,
                                             Thang = thang,
                                             Nam = nam,
                                             CongLamViec = (double)a.CongLamViec,
                                             LuongLamViec = (int)b.Luong,
                                             CongTangCa = (double)a.CongTangCa,
                                             LuongTangCa = (int)b.LuongTangCa,
                                             LuongPhuCap = (int)b.PhuCap,
                                             HieuSuat = (double)a.HieuSuatLamViec,
                                             TongLuong = (int)b.TongLuongThang
                                         }).ToList();
                return ls;
            }

        }

        public List<DGVBANGLUONG> LayBangLuongNV(int thang, int nam, int manv)
        {
            if (thang != 0)
            {
                List<DGVBANGLUONG> ls = (from a in DB.TONGCONGTHANGs
                                         from b in DB.CT_LUONGTHANGs
                                         where a.MaNV == b.MaNV && a.Thang == thang && b.Thang == thang && a.Nam == nam && a.MaNV == manv && b.MaNV == manv
                                         select new DGVBANGLUONG()
                                         {
                                             MaNV = a.MaNV,
                                             TenNV = b.NHANVIEN.HoTen,
                                             Thang = a.Thang,
                                             Nam = a.Nam,
                                             CongLamViec = (double)a.CongLamViec,
                                             LuongLamViec = (int)b.Luong,
                                             CongTangCa = (double)a.CongTangCa,
                                             LuongTangCa = (int)b.LuongTangCa,
                                             LuongPhuCap = (int)b.PhuCap,
                                             HieuSuat = (double)a.HieuSuatLamViec,
                                             TongLuong = (int)b.TongLuongThang
                                         }).Distinct().ToList();
                return ls;
            }
            else
            {
                List<DGVBANGLUONG> ls = (from a in DB.TONGCONGTHANGs
                                         from b in DB.CT_LUONGTHANGs
                                         where a.MaNV == b.MaNV && a.Nam == nam && a.MaNV == manv && b.MaNV == manv && a.Thang == b.Thang 
                                         select new DGVBANGLUONG()
                                         {
                                             MaNV = a.MaNV,
                                             TenNV = b.NHANVIEN.HoTen,
                                             Thang = a.Thang,
                                             Nam = a.Nam,
                                             CongLamViec = (double)a.CongLamViec,
                                             LuongLamViec = (int)b.Luong,
                                             CongTangCa = (double)a.CongTangCa,
                                             LuongTangCa = (int)b.LuongTangCa,
                                             LuongPhuCap = (int)b.PhuCap,
                                             HieuSuat = (double)a.HieuSuatLamViec,
                                             TongLuong = (int)b.TongLuongThang
                                         }).Distinct().ToList();
                return ls;
            }
        }

        //public IQueryable<DGVBANGLUONG> LayBangLuong2(int thang, int nam, int mapb, string hoten)
        //{
        //    if (mapb == 0 && hoten == "")
        //    {
        //        IQueryable<DGVBANGLUONG> ls = from a in DB.TONGCONGTHANGs
        //                                      from b in DB.CT_LUONGTHANGs
        //                                      where a.MaNV == b.MaNV && b.Thang == thang && a.Thang == thang && a.Nam == nam && b.Nam == nam
        //                                      select new DGVBANGLUONG()
        //                                      {
        //                                          MaNV = a.MaNV,
        //                                          TenNV = b.NHANVIEN.HoTen,
        //                                          CongLamViec = (double)a.CongLamViec,
        //                                          LuongLamViec = (int)b.Luong,
        //                                          CongTangCa = (double)a.CongTangCa,
        //                                          LuongTangCa = (int)b.LuongTangCa,
        //                                          LuongPhuCap = (int)b.PhuCap,
        //                                          HieuSuat = (double)a.HieuSuatLamViec,
        //                                          TongLuong = (int)b.TongLuongThang
        //                                      };
        //        return ls;
        //    }
        //    else if (mapb == 0 && hoten != "")
        //    {
        //        IQueryable<DGVBANGLUONG> ls = from a in DB.TONGCONGTHANGs
        //                                      from b in DB.CT_LUONGTHANGs
        //                                      where a.MaNV == b.MaNV && b.Thang == thang && a.Thang == thang && a.Nam == nam && b.Nam == nam && a.NHANVIEN.HoTen.Contains(@hoten)
        //                                      select new DGVBANGLUONG()
        //                                      {
        //                                          MaNV = a.MaNV,
        //                                          TenNV = b.NHANVIEN.HoTen,
        //                                          CongLamViec = (double)a.CongLamViec,
        //                                          LuongLamViec = (int)b.Luong,
        //                                          CongTangCa = (double)a.CongTangCa,
        //                                          LuongTangCa = (int)b.LuongTangCa,
        //                                          LuongPhuCap = (int)b.PhuCap,
        //                                          HieuSuat = (double)a.HieuSuatLamViec,
        //                                          TongLuong = (int)b.TongLuongThang
        //                                      };

        //        return ls;

        //    }
        //    else if (hoten == "" && mapb != 0)
        //    {
        //        IQueryable<DGVBANGLUONG> ls = from a in DB.TONGCONGTHANGs
        //                                      from b in DB.CT_LUONGTHANGs
        //                                      where a.MaNV == b.MaNV && b.Thang == thang && a.Thang == thang && a.Nam == nam && b.Nam == nam && a.NHANVIEN.MaPB == mapb
        //                                      select new DGVBANGLUONG()
        //                                      {
        //                                          MaNV = a.MaNV,
        //                                          TenNV = b.NHANVIEN.HoTen,
        //                                          CongLamViec = (double)a.CongLamViec,
        //                                          LuongLamViec = (int)b.Luong,
        //                                          CongTangCa = (double)a.CongTangCa,
        //                                          LuongTangCa = (int)b.LuongTangCa,
        //                                          LuongPhuCap = (int)b.PhuCap,
        //                                          HieuSuat = (double)a.HieuSuatLamViec,
        //                                          TongLuong = (int)b.TongLuongThang
        //                                      };

        //        return ls;
        //    }
        //    else
        //    {
        //        IQueryable<DGVBANGLUONG> ls = from a in DB.TONGCONGTHANGs
        //                                      from b in DB.CT_LUONGTHANGs
        //                                      where a.MaNV == b.MaNV && b.Thang == thang && a.Thang == thang && a.Nam == nam && b.Nam == nam && a.NHANVIEN.MaPB == mapb && a.NHANVIEN.HoTen.Contains(@hoten)
        //                                      select new DGVBANGLUONG()
        //                                      {
        //                                          MaNV = a.MaNV,
        //                                          TenNV = b.NHANVIEN.HoTen,
        //                                          CongLamViec = (double)a.CongLamViec,
        //                                          LuongLamViec = (int)b.Luong,
        //                                          CongTangCa = (double)a.CongTangCa,
        //                                          LuongTangCa = (int)b.LuongTangCa,
        //                                          LuongPhuCap = (int)b.PhuCap,
        //                                          HieuSuat = (double)a.HieuSuatLamViec,
        //                                          TongLuong = (int)b.TongLuongThang
        //                                      };
        //        return ls;
        //    }

        //}

    }
}
